var t = getApp();

Page({
  data: {
    onPatientFlag: !1,
    details:[
      {
        groupId:1,
        teamIndex:2,
        groupCheck:'/assets/images/check02.png',
        groupUnfoldFlag: '/assets/images/check02.png',
        patients:[
          {

          }
        ]
      },
      {
        groupId: 1,
        teamIndex: 2,
        groupCheck: '/assets/images/check02.png',
        groupUnfoldFlag: '/assets/images/check02.png',
        patients: [
          {

          }
        ]
      }
    ]
  },
  onLoad: function(a) {
    // var e = this;
    // this.setData({
    //   sourceType: a.sourceType || "",
    //   token: wx.getStorageSync("log"),
    //   patientList: wx.getStorageSync("patientList"),
    //   patientType: a.patientType || ""
    // }), e.data.patientList ? e.setData({
    //   patientList: wx.getStorageSync("patientList")
    // })
    //  : (wx.showLoading({
    //   title: "加载中...",
    //   mask: !0
    // }), 
    // wx.request({
    //   url: t.globalData.dic + "doctor/manage/list/patient",
    //   data: {
    //     token: e.data.token,
    //     type: 2
    //   },
    //   success: function(t) {
    //     console.log(t), wx.hideLoading(), 200 == t.data.code && (null != t.data.data.groups && t.data.data.groups.length > 0 && (null != t.data.data.unGroupPatientList || t.data.data.unGroupPatientList.length > 0) && (t.data.data.groups.forEach(function(t, a) {
    //       t.groupCheck = !1, t.groupUnfoldFlag = !1, t.teamIndex = 0;
    //       var e = a;
    //       null !== t.patients && t.patients.length > 0 && t.patients.forEach(function(t) {
    //         t.patientCheck = !1, t.groupIndex = e, t.teamIndex = 0;
    //       });
    //     }), t.data.data.unGroupPatientList.forEach(function(t) {
    //       t.patientCheck = !1, t.teamIndex = 0;
    //     })), null != t.data.data.teams && t.data.data.teams.length > 0 && t.data.data.teams.forEach(function(t, a) {
    //       t.teamCheck = !1;
    //       var e = a;
    //       t.teamUnfoldFlag = !1, t.groups.length > 0 && t.groups.forEach(function(t, a) {
    //         t.groupCheck = !1, t.groupUnfoldFlag = !1, t.teamIndex = e + 1;
    //         var i = a;
    //         null !== t.patients && t.patients.length > 0 && t.patients.forEach(function(t) {
    //           t.patientCheck = !1, t.groupIndex = i, t.teamIndex = e + 1;
    //         });
    //       }), t.unGroupPatients && t.unGroupPatients.forEach(function(t) {
    //         t.patientCheck = !1, t.teamIndex = e + 1;
    //       });
    //     }), t.data.data.teams.unshift({
    //       teamName: "个人",
    //       teamCheck: !1,
    //       teamUnfoldFlag: !1,
    //       unGroupPatients: t.data.data.unGroupPatientList,
    //       groups: t.data.data.groups
    //     }), e.setData({
    //       patientList: t.data.data
    //     }), console.log(e.data.patientList));
    //   }
    // }));
  },
  onReady: function() {},
  onShow: function() {
    wx.getStorageSync("patientList") && this.setData({
      patientList: wx.getStorageSync("patientList")
    });
  },
  onHide: function() {},
  onUnload: function() {
    1 != this.data.sourceType && wx.removeStorageSync("patientList");
  },
  groupUnfold: function(t) {
    var a = parseInt(t.currentTarget.dataset.index),
      e = parseInt(t.currentTarget.dataset.teamindex);
    if (1 == t.currentTarget.dataset.current) return this.data.patientList.teams[a].teamUnfoldFlag = !this.data.patientList.teams[a].teamUnfoldFlag,
      void this.setData({
        patientList: this.data.patientList
      });
    this.data.patientList.teams[e].groups[a].groupUnfoldFlag ? (this.data.patientList.teams[e].groups[a].groupUnfoldFlag = !1,
      this.setData({
        patientList: this.data.patientList
      })) : (this.data.patientList.teams[e].groups[a].groupUnfoldFlag = !0, this.setData({
      patientList: this.data.patientList
    }));
  },
  checkPatient: function(t) {
    var a = this;
    if (console.log(t), 0 == t.target.dataset.targettype) {
      n = t.target.dataset.index;
      this.data.patientList.teams[n].teamCheck = !this.data.patientList.teams[n].teamCheck,
        this.data.patientList.teams[n].groups.forEach(function(t) {
          t.groupCheck = a.data.patientList.teams[n].teamCheck, null != t.patients && t.patients.length > 0 && t.patients.forEach(function(t) {
            t.patientCheck = a.data.patientList.teams[n].teamCheck;
          });
        }), null != this.data.patientList.teams[n].unGroupPatients && this.data.patientList.teams[n].unGroupPatients.length > 0 && this.data.patientList.teams[n].unGroupPatients.forEach(function(t) {
          t.patientCheck = a.data.patientList.teams[n].teamCheck;
        });
    }
    if (1 == t.target.dataset.targettype) {
      var e = t.target.dataset.teamindex,
        i = t.target.dataset.index,
        s = !0;
      this.data.patientList.teams[e].groups[i].groupCheck = !this.data.patientList.teams[e].groups[i].groupCheck,
        null !== this.data.patientList.teams[e].groups[i].patients && this.data.patientList.teams[e].groups[i].patients.length > 0 && this.data.patientList.teams[e].groups[i].patients.forEach(function(t) {
          t.patientCheck = a.data.patientList.teams[e].groups[i].groupCheck;
        });
      for (d = 0; d < this.data.patientList.teams[e].groups.length; d++)
        if (!this.data.patientList.teams[e].groups[d].groupCheck) {
          s = !1;
          break;
        }
      for (d = 0; d < this.data.patientList.teams[e].unGroupPatients.length; d++)
        if (!this.data.patientList.teams[e].unGroupPatients[d].patientCheck) {
          s = !1;
          break;
        }
      this.data.patientList.teams[e].teamCheck = s;
    }
    if (2 == t.target.dataset.targettype) {
      var e = t.target.dataset.teamindex,
        i = t.target.dataset.groupindex,
        n = t.target.dataset.index,
        s = !0,
        p = !0,
        r = this.data.patientList.teams[e].groups[i].patients[n].patientCheck;
      this.data.patientList.teams[e].groups[i].patients[n].patientCheck = !r;
      for (d = 0; d < this.data.patientList.teams[e].groups[i].patients.length; d++)
        if (!this.data.patientList.teams[e].groups[i].patients[d].patientCheck) {
          p = !1;
          break;
        }
      this.data.patientList.teams[e].groups[i].groupCheck = p;
      for (d = 0; d < this.data.patientList.teams[e].groups.length; d++)
        if (!this.data.patientList.teams[e].groups[d].groupCheck) {
          s = !1;
          break;
        }
      for (d = 0; d < this.data.patientList.teams[e].unGroupPatients.length; d++)
        if (!this.data.patientList.teams[e].unGroupPatients[d].patientCheck) {
          s = !1;
          break;
        }
      this.data.patientList.teams[e].teamCheck = s;
    }
    if (3 == t.target.dataset.targettype) {
      var e = t.target.dataset.teamindex,
        n = t.target.dataset.index,
        s = !0;
      this.data.patientList.teams[e].unGroupPatients[n].patientCheck = !this.data.patientList.teams[e].unGroupPatients[n].patientCheck;
      for (d = 0; d < this.data.patientList.teams[e].groups.length; d++)
        if (!this.data.patientList.teams[e].groups[d].groupCheck) {
          s = !1;
          break;
        }
      for (d = 0; d < this.data.patientList.teams[e].unGroupPatients.length; d++)
        if (!this.data.patientList.teams[e].unGroupPatients[d].patientCheck) {
          s = !1;
          break;
        }
      this.data.patientList.teams[e].teamCheck = s;
    }
    if (4 == t.target.dataset.targettype) {
      var o = t.target.dataset.userid,
        r = this.data.searchPersonList[t.target.dataset.index].patientCheck;
      this.data.searchPersonList[t.target.dataset.index].patientCheck = !r, this.setData({
        searchPersonList: this.data.searchPersonList
      }), console.log(222);
      for (var d = 0; d < this.data.patientList.teams.length; d++) {
        console.log("循环团队");
        s = !0;
        if (this.data.patientList.teams[d].groups.length > 0) this.data.patientList.teams[d].groups.forEach(function(t) {
          var a = !0;
          null != t.patients && t.patients.length > 0 ? t.patients.forEach(function(t) {
            t.userId == o && (t.patientCheck = !r), t.patientCheck || (a = !1, s = !1);
          }) : (a = !1, s = !1), t.groupCheck = a, t.groupCheck || (s = !1);
        });
        else s = !1;
        if (this.data.patientList.teams[d].unGroupPatients.length > 0) this.data.patientList.teams[d].unGroupPatients.forEach(function(t) {
          t.userId == o && (t.patientCheck = !r), t.patientCheck || (s = !1);
        });
        else if (!s) s = !1;
        this.data.patientList.teams[d].teamCheck = s;
      }
    }
    this.setData({
      patientList: this.data.patientList
    });
  },
  nextBtn: function() {
    wx.setStorageSync("patientList", this.data.patientList), wx.navigateBack({
      delta: 1
    });
  },
  search: function(a) {
    var e = this,
      i = a.detail.value;
    this.setData({
      searchFlag: !0,
      searchOnPatent: !1
    }), 0 != i.replace(/(^\s*)|(\s*$)/g, "").length ? wx.request({
      url: t.globalData.dic + "doctor/manage/all/searchPatient",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        token: this.data.token,
        keyWord: i
      },
      success: function(t) {
        console.log(t), 200 == t.data.code && (t.data.data.length > 0 && null != t.data.data ? (t.data.data.forEach(function(a, i) {
          var s = a.userId,
            i = e.data.patientList.teams.forEach(function(a) {
              if (null != a.groups && a.groups.length > 0 && a.groups.forEach(function(a) {
                  if (null != a.patients && a.patients.length > 0)
                    for (var e = 0; e < a.patients.length; e++)
                      if (a.patients[e].userId == s) {
                        t.data.data[i].patientCheck = a.patients[e].patientCheck;
                        break;
                      }
                }), null != a.unGroupPatients && a.unGroupPatients.length > 0)
                for (var e = 0; e < a.unGroupPatients.length; e++)
                  if (a.unGroupPatients[e].userId == s) {
                    t.data.data[i].patientCheck = a.unGroupPatients[e].patientCheck;
                    break;
                  }
            });
        }), e.setData({
          searchPersonList: t.data.data,
          searchOnPatent: !1
        })) : e.setData({
          searchOnPatent: !0,
          searchPersonList: t.data.data
        }));
      }
    }) : this.setData({
      searchFlag: !1,
      searchOnPatent: !1
    });
  }
});